import * as Cesium from "cesium";
import stationModel from '/src/assets/model/station.gltf'
export function initEntities($viewer,datas) {
  // console.log(datas)
  let polyline_positions = [];
  const x_arr = datas.xs.split(",");
  const y_arr = datas.ys.split(",");
  x_arr.forEach((item, index) => {
    polyline_positions.push(Number(item));
    polyline_positions.push(Number(y_arr[index]));
  });
  const positions = Cesium.Cartesian3.fromDegreesArray(polyline_positions);
  const line = $viewer.entities.add({
    polyline: {
      positions, 
      material: Cesium.Color.WHITE.withAlpha(0.8),
      width: 10,
      clampToGround: true, // 贴地
    },
  });
  $viewer.zoomTo(line);
  datas.stations.forEach((item, index) => {
    const position = Cesium.Cartesian3.fromDegrees(
      ...item.xy_coords.split(";").map((item) => Number(item))
    );
    const orientation = Cesium.Transforms.headingPitchRollQuaternion(
      position,
      Cesium.HeadingPitchRoll.fromDegrees(-90, 0, 0) // 摇头 点头 翻转（歪头）
    );
    // console.log(orientation);
    let station = $viewer.entities.add({
      position,
      orientation,
      label: {
        text: item.name,
        font: "10px Helvetica",
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        fillColor: Cesium.Color.fromCssColorString("#fff"), // 字体颜色
        showBackground: true,
        backgroundColor: Cesium.Color.fromCssColorString("#000").withAlpha(0.5), // 背景颜色
        // pixelOffset: new Cesium.Cartesian2(0, -50),
        eyeOffset: new Cesium.Cartesian3(0, 21, -10), // 在相机的场景坐标系中的偏移
      },
      model: {
        // uri: "/src/assets/model/station.gltf",// 不行
        uri: stationModel,
        scale: 0.07, // 模型缩放倍数
        minimumPixelSize: 30, // 模型最小像素尺寸
      },
      id: "station" + index,
    });
    // 设置 depthTest 为 false，使标签忽略深度测试
    // station.label.depthTest = false;
  });
  return { positions,line }
}
